package hanjia;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2025/2/11-下午8:51
 * Since  :   1.0
 * Version:   1.0
 */
public class LeetCode617 {

    //自己想的
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {

        if (root1==null&&root2==null){
            return null;
        }
       int left=0;
       int right=0;
       TreeNode leftNode1=null;
       TreeNode leftNode2=null;
       TreeNode rightNode1=null;
       TreeNode rightNode2=null;

       if (root1!=null){
           left=root1.val;
           leftNode1=root1.left;
           rightNode1=root1.right;

       }
       if (root2!=null){
           right= root2.val;
           leftNode2=root2.left;
           rightNode2=root2.right;
       }






        TreeNode treeNode = new TreeNode(left+right);
        treeNode.left=mergeTrees(leftNode1,leftNode2);
        treeNode.right=mergeTrees(rightNode1,rightNode2);
        return treeNode;




    }
//题解
    public TreeNode mergeTrees2(TreeNode root1, TreeNode root2){
        if(root1==null){
            return root2;
        }
        if(root2==null){
            return root1;
        }
        TreeNode root=new TreeNode(0);
        root.val=root1.val+root2.val;
        root.left=mergeTrees2(root1.left,root2.left);
        root.right=mergeTrees2(root1.right,root2.right);
        return root;
    }
    }

